package org.csu.jpetmanage.controller;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import jakarta.servlet.http.HttpSession;
import org.csu.jpetmanage.mapper.InventoryMapper;
import org.csu.jpetmanage.model.*;
import org.csu.jpetmanage.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class InventoryController {
    @Autowired
    InventoryService inventoryService;

    @Autowired
    Inventory inventory;

    @Autowired
    Inventory inventory1;
    @Autowired
    InventoryMapper inventoryMapper;
    @GetMapping("/inventory")
    public String inventory(Model model)
    {

        List<Inventory>inventoryList=new ArrayList<>();
        inventoryList=inventoryMapper.selectList(null);
        model.addAttribute("inventoryList",inventoryList);
        return "inventory";
    }

    /**
     *
     * 实现添加功能
     */
    //@RequestParam("category") String category,
    @RequestMapping("/inventoryAdd")
    public String  inventoryAdd(@RequestParam("itemid") String itemid,
                              @RequestParam("qty") int qty,
                              Model model,
                              HttpSession session)
    {
        inventory.setQty(qty);
        inventory.setItemid(itemid);
        inventoryMapper.insert(inventory);
        session.setAttribute("msg","插入成功");
        return "redirect:inventory";
    }

    /**
     *
     * 删除小类
     */
    @GetMapping("/inventoryDelete")
    public String delete(@RequestParam("itemid") String itemid, Model model)
    {
        QueryWrapper<Inventory> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("itemid",itemid);
        inventoryMapper.delete(queryWrapper);
        int a = inventoryMapper.deleteById(itemid);
        return "redirect:/inventory";

    }

    /**
     *
     * 修改小类
     */
    @GetMapping("/inventoryEditAjax")
    @ResponseBody
    public Map<String,Object> inventoryEditAjax(
            @RequestParam("itemid") String itemid,
            HttpSession session
    ){
        QueryWrapper<Inventory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemid",itemid);//是根据Order实体类中的属性来进行取值
        List<Inventory> inventoryList =inventoryMapper.selectList(queryWrapper);
        Map<String, Object> map = new HashMap<>();
        if (inventoryList.size()==1){
            System.out.println("inventoryList size is 1");
            inventory = inventoryList.get(0);
            session.setAttribute("editInventory",inventory);//待会处理编辑数据的时候需要用
            //把下面补全
            map.put("itemid",inventory.getItemid());
            map.put("qty",inventory.getQty());
            return map;
        }
        return map;
    }

    @RequestMapping("/inventoryEdit")
    public String inventoryEdit(
            @RequestParam("editItemid") String itemid,
            @RequestParam("editQty") int qty,
            Model model, HttpSession session
    ){
        inventory.setItemid(itemid);
        inventory.setQty(qty);
        UpdateWrapper<Inventory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("itemid",inventory.getItemid());
        int rows = inventoryMapper.update(inventory,updateWrapper);
        System.out.println("Edited rows number is: "+rows);
        session.setAttribute("msg","修改成功");
        return "redirect:/inventory";
    }

    @RequestMapping("/inventorySearch")
    public String inventorySearch(
            @RequestParam("keyword") String keyword,
            Model model){
        QueryWrapper<Inventory> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("itemid",keyword).
                or().like("qty",keyword);
        List<Inventory> inventoryList=inventoryMapper.selectList(queryWrapper);
        model.addAttribute("inventoryList" ,inventoryList);
        return "inventory";
    }

    @GetMapping("/inventorySearchAjax")
    @ResponseBody
    public List<Inventory> inventorySearchAjax(
            @RequestParam("keyword") String keyword,
            HttpSession session){
        System.out.println(keyword+" in OrderController");
        QueryWrapper<Inventory> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("itemid",keyword).
                or().like("qty",keyword);
        return inventoryMapper.selectList(queryWrapper);
    }
}
